$(document).ready(function () {
//    $("#user_acceptance").attr("checked", "checked");
    $('<input>').attr({
        type:'hidden',
        id:'check_info',
        name:'check_info'
    }).appendTo('form');
    $("input").focusin(function () {
//        $(this).css("border","1px solid #3BA0E4");
//        $(this).parent().next().html("");
    });
    $("#user_login").focusout(function () {
        check_login($(this));
    });
    $("#user_password").focusout(function () {
        check_password($(this).val());
    });
    $("#user_password_confirmation").focusout(function () {
        check_password_confirmation($("#user_password").val(), $(this).val());
    });
    $("#user_email").focusout(function () {
        check_email($(this));
    });
    $("#user_acceptance").click(function () {
        check_acceptance($(this).attr("checked"));
    });
    $("#create_shipper_user").click(function () {

        var acc_flag = check_acceptance($("#user_acceptance").attr("checked"));
        var login_flag = check_login($("#user_login"));
        if (login_flag == undefined)
            login_flag = ($("#user_login").next().val() == "true") ? true : false;
        var password_flag = check_password($("#user_password").val());
        var confirm_flag = check_password_confirmation($("#user_password").val(), $("#user_password_confirmation").val());
        var email_flag = check_email($("#user_email"));
        if (email_flag == undefined)
            email_flag = ($("#user_email").next().val() == "true") ? true : false;
        var flag = acc_flag && login_flag && password_flag && confirm_flag && email_flag
        if (flag) {
            $("#block_div").css("width", $("#shipper_user_div").width());
            $("#block_div").css("height", $("#shipper_user_div").height());
            $("#block_div").css("top", $("#shipper_user_div").offset().top);
            $("#block_div").show();
//            try_submit(flag);
            $("#shipper_user_div").children("form").submit();
        }

        return false;
    });
});

function check_login(login) {
    var val = login.val();
    var flag = false;
    if (val.length == 0) {
        $("#user_login_msg").html("请填写您的用户帐号");
        return flag;
    } else if (val.length < 4 || val.length > 16) {
        $("#user_login_msg").html("用户名长度应为6~16个字符");
        return flag;
    } else {
        if (login.next().length == 0) {
            $('<input>').attr({
                type:'hidden',
                name:'check_login'
            }).insertAfter(login);
        }
        $.post("/users/check_login", { login:val }, function (data) {
            $("#user_login_msg").html(data.notice);
            flag = ( data.state == "success" );
            if (flag)
                login.next().val('true')
            else
                login.next().val('false')
        }, "json");
    }

}


function check_password(password) {
    $("#user_password_msg").html("");
    var flag = false;
    if (password.length == 0) {
        $("#user_password_msg").html("请填写您的密码");
    } else if (password.length < 6 || password.length > 16) {
        $("#user_password_msg").html("密码长度应为6~16个字符");
    } else {
        flag = true;
    }
    return flag;
}

function check_password_confirmation(password, password_confirmation) {
    $("#user_password_confirmation_msg").html("");
    var flag = true;
    if (password != password_confirmation) {
        flag = false;
        $("#user_password_confirmation_msg").html("两次输入的密码不一致");
    }
    return flag;
}

function check_email(email) {
    var flag = false;
    var email_reg = /^[0-9a-zA-Z_\-\.]+@[0-9a-zA-Z_\-]+(\.[0-9a-zA-Z_\-]+)*$/;
    var email_val = email.val();
    var old_email_val = email.next().next().val();
    if (email_val == old_email_val && email_val) {
        $("#user_email_msg").html("");
        return true;
    }
    if (email_val.length == 0) {
        $("#user_email_msg").html("请填写您的email");
        return flag;
    } else if (!email_reg.test(email_val)) {
        $("#user_email_msg").html("email格式不正确");
        return flag;
    } else {
        if (email.next().length == 0) {
            $('<input>').attr({
                type:'hidden',
                name:'check_email'
            }).insertAfter(email);
        }
        $.post("/users/check_email", { email:email_val }, function (data) {
            $("#user_email_msg").html(data.notice);
            flag = (data.state == "success");
            if (flag)
                email.next().val('true');
            else
                email.next().val('false');
        }, "json");
    }


}

function check_acceptance(accptance) {
    var flag = true;
    if (accptance) {
        $("#user_acceptance_msg").html("");
    } else {
        flag = false;
        $("#user_acceptance_msg").html("请接受服务协议");
    }
    return flag;
}
